<?php
class AdminAction extends Action {
	/**
	 * 后台登陆页面
	 */
	public function login(){
		$this->display("login");
	}
	/**
	 * 检测登录
	 */
	public function checkLogin(){
		$username=$_REQUEST["user"];
		$pwd=$_REQUEST["pwd"];
		$query["user_name"]=$username;
		//判断用户是否存在
		$user=new Model("user");
		$data=$user->where($query)->select();
		if(!$data){
			echo "1";//用户不存在
			exit;
		}
		if($data[0]["user_pwd"]!=$pwd){
			echo "2";//密码错误
			exit;
		}
		$_SESSION["user_id"]=$data[0]["pid"];
		$_SESSION["user_name"]=$username;
		echo "3";
		exit;
	}
	/**
	 * 后台主页面
	 */
	public function main(){
		$this->display("main");
	}
	public function top(){
		$this->assign('username',$_SESSION["user_name"]);
		$this->display("top");
	}
	public function left(){
		$this->display("left");
	}
	public function right(){
		$this->display("right");
	}
	public function down(){
		$this->display("down");
	}
	public function center(){
		$this->display("center");
	}
	/**
	* 说明：用户管理
	* 参数：...
	* 作者：df 
	* 时间：2012-3-25
	**/
	public function userIndex(){
		$this->display("userIndex");
	}
	/**
	 * 得到晕乎数据
	 * Enter description here ...
	 */
	public function getUserInfo(){
		
		//echo $str;
		$arr=array();
		$user=new Model("user");
		$data=$user->select();
		if(is_array($data) && count($data)>0){
			$i=0;
			foreach ($data as $row){
				$arr[$i][]=$row["pid"];
				//$arr[$i][]="<input type='checkbox' name='userCheck' id='userCheck_".$row["pid"]."' value='".$row["pid"]."'>";
				$arr[$i][]=$row["user_name"];
				$arr[$i][]=$row["user_pwd"];
				$arr[$i][]=$row["user_phone"];
				$arr[$i][]=$row["user_created_time"]?date("Y-m-d H:i",$row["user_created_time"]):"";
				if($row["user_name"]=="admin"){
					$arr[$i][]="<a style='cursor:pointer' id='editUser' onclick='editUser(this.pid)' pid='".$row["pid"]."'>[编辑]</a>";
				}else{
					$arr[$i][]="<a style='cursor:pointer' id='editUser' onclick='editUser(this.pid)' pid='".$row["pid"]."'>[编辑]</a>&nbsp;&nbsp;<a style='cursor:pointer' onclick='delUser(this.pid)' id='delUser' pid='".$row["pid"]."'>[删除]</a>";
				}
				
				$i++;
			}
		}
		$out["aaData"]=$arr;
		echo json_encode($out);
		exit;
	}
	/**
	 * 编辑用户
	 */
	public function userEdit(){
		$pid=$_GET["pid"];
		$user=new Model("user");
		$find["pid"]=$pid;
		$data=$user->where($find)->select();
		$this->assign("data",$data[0]);
		$this->display('userEdit');
	}
	/**
	 * 保存编辑用户的信息
	 */
	public function saveEdit(){
		
		$data=$_REQUEST["data"];
		$user=new Model("user");
		$user->find($data["pid"]);
		//$data["user_created_time"]=time();
		$rev=$user->save($data);
		echo $rev;
		exit;
		
	}
	/**
	 * 添加用户信息
	 */
	public function userAdd(){
		$this->display("userAdd");
	}
	/**
	 * 保存添加的用户
	 * 
	 */
	public function saveAdd(){
		$data=$_REQUEST["data"];
		$user=new Model("user");
		$data["user_created_time"]=time();
		$rev=$user->add($data);
		echo $rev;
		exit;
	}
	/**
	 * 删除用户
	 */
	public function userDel(){
		$pid=$_GET["pid"];
		$user=new Model("user");
		$rev=$user->where('pid='.$pid)->delete();
		echo $rev;
		exit;
	}
	/**
	 * 图片管理
	 * Enter description here ...
	 */
	public function image(){
		$image=new Model("image_group");
		$data=$image->select();
		$this->assign("data",$data);
		$qu["img_bz"]="1";
		$images=new Model("image");
		$data=$images->where($qu)->select();
		$outData=array();
		if(is_array($data) && count($data)){
			$i=0;
			foreach ($data as $row){
				$outData[$i]["pid"]=$row["pid"];
				$outData[$i]["img_name"]=$row["img_name"];
				$outData[$i]["img_url"]=$row["img_url"];
				$outData[$i]["img_time"]=date("Y-m-d",$row["img_time"]);
				$outData[$i]["img_shuoming"]=$row["img_shuoming"];
				
				$g_id=$row["g_id"];
				$query["g_id"]=array('in',$g_id);
				$imageg=new Model("image_group");
				$g_name=$imageg->field('g_name_cn')->where($query)->select();
				$str_name='';
				if(is_array($g_name) && count($g_name)>0){
					foreach ($g_name as $r){
						if($str_name==''){
							$str_name=$r["g_name_cn"];
						}else{
							$str_name=$str_name." ".$r["g_name_cn"];
						}
					}
				}
				$outData[$i]["g_name"]=$str_name;
				$i++;
			}
		}
		$this->assign("imageData",$outData);
		$this->display("image");
	}
	/**
	 * 得到图片信息
	 */
	public function imageInfo(){
		//echo $str;
		$arr=array();
		$user=new Model("image");
		$query["img_bz"]="1";
		$data=$user->where($query)->select();
		if(is_array($data) && count($data)>0){
			$i=0;
			foreach ($data as $row){
				$arr[$i][]=$row["pid"];
				//$arr[$i][]="<input type='checkbox' name='userCheck' id='userCheck_".$row["pid"]."' value='".$row["pid"]."'>";
				$arr[$i][]=$row["img_name"];
				$arr[$i][]=$row["img_url"];
				$arr[$i][]=$row["img_time"]?date("Y-m-d H:i",$row["img_time"]):"";
				$arr[$i][]=$row["img_shuoming"];
				$arr[$i][]="<a style='cursor:pointer' id='editUser' onclick='editUser(this.pid)' pid='".$row["pid"]."'>[查看]</a>&nbsp;&nbsp;<a style='cursor:pointer' onclick='delUser(this.pid)' id='delUser' pid='".$row["pid"]."'>[删除]</a>";
				
				$i++;
			}
		}
		$out["aaData"]=$arr;
		echo json_encode($out);
		exit;
	}
	/**
	 * 添加图片信息
	 */
	public function imageAdd(){
		$file_size=ini_get('upload_max_filesize');
		$file_size=str_replace("M","",$file_size);
		$this->assign('MaxFileSize',$file_size);
		$manURL=$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']));
		$this->assign("manUrl","http://".$manURL."/Public/upload.php");
		$gid=$_GET["g_id"];
		$this->assign('gid',$gid);
		$this->display("imageAdd");
	}
	/**
	 * 保存上传的图片信息
	 */
	public function imageSave(){
		//$data=$_POST["data"];
		//$gid=$data["g_id"];
		//$imageInfo=$data["imageInfo"];
		$gid=$_REQUEST["g_id"];
		$imageInfo=$_REQUEST["imageInfo"];
		$img_path=C("IMAGE_UPLOAD_DIR");
		$rev='0';
		if($imageInfo){
			$imageInfo=explode('|', $imageInfo);
			if(is_array($imageInfo) && count($imageInfo)){
				$rev='1';
				foreach ($imageInfo as $im){
					$sData["img_name"]=$im;
					$sData["img_url"]=$img_path.$im;
					$sData["img_time"]=time();
					$sData["img_shuoming"]=$im;
					$sData["g_id"]=$gid;
					$sData["img_bz"]='1';
					$image=new Model("image");
					$rev=$image->add($sData);
				}
			}
		}
		echo $rev;
		exit;
	}
	/**
	 * 删除图片
	 */
	public function imageDel(){
		$pid=$_GET["pid"];
		$user=new Model("image");
		$rev=$user->where('pid='.$pid)->delete();
		echo $rev;
		exit;
	}
	/**
	 * 添加图片分类
	 */
	public function groupAdd(){
		$this->display("groupAdd");
	}
	/**
	 * 保存图片分类
	 */
	public function groupSave(){
		$data=$_REQUEST["data"];
		$user=new Model("image_group");
		if($data["g_id"]){
			$user->find($data["g_id"]);
			$rev=$user->save($data);
		}else{
			$data["g_created_time"]=time();
			$rev=$user->add($data);
		}
		echo $rev;
		exit;
	}
	/**
	 * 删除图片分类
	 */
	public function groupDel(){
		$pid=$_GET["gid"];
		$user=new Model("image_group");
		$rev=$user->where('g_id='.$pid)->delete();
		echo $rev;
		exit;
	}
	/**
	 * 编辑图片分类
	 */
	public function groupEdit(){
		$pid=$_GET["g_id"];
		$img=new Model("image_group");
		$find["g_id"]=$pid;
		$data=$img->where($find)->select();
		$this->assign("data",$data[0]);
		$this->display("groupEdit");
	}
	/**
	 * 退出系统
	 */
	public function logout(){
		
		unset($_SESSION["user_id"]);
		$_SESSION='';
		$adminUrl=$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']));
		header("Location: http://".$adminUrl."/?m=Admin&a=login");
	}
	/**
	 * 菜单管理
	 */
	public function menu(){
		$menu=new Model("menu");
		
		$data=$menu->select();
		$this->assign("menu",$data);
		$this->display("menu");
	}
	/**
	 * 分类管理(中文)
	 */
	public function editfenlei(){
		$id=$_REQUEST["m_id"];
		$menu=new Model("menu");
	
		$find["m_id"]=$id;
		$data=$menu->where($find)->select();
		$this->assign("m_id",$id);
		$out=$data[0];
		$out["m_text_cn"]=stripslashes($out["m_text_cn"]);
		$this->assign("menu",$out);
		$this->display("editfenlei");
	}
	/**
	 * 分类管理(英文)
	 */
	public function editfenleien(){
		$id=$_REQUEST["m_id"];
		$menu=new Model("menu");
	
		$find["m_id"]=$id;
		$data=$menu->where($find)->select();
		$this->assign("m_id",$id);
		$out=$data[0];
		$out["m_text_en"]=stripslashes($out["m_text_en"]);
		$this->assign("menu",$out);
		$this->display("editfenleien");
	}
	/**
	 * 保存分类
	 */
	public function savefenlei(){
		$data=$_REQUEST["data"];
		$menu=new Model("menu");
		if($data["m_id"]){
			$menu->find($data["m_id"]);
			$rev=$menu->save($data);
		}
		echo $rev;
		exit;
	}
	/**
	 * 墙体彩绘
	 */
	public function qiangti(){
		$image=new Model("image");
		$query["img_bz"]='2';
		$datas=$image->where($query)->select();
		$this->assign("data",$datas);
		$this->display("qiangti");
	}
	/**
	 * 上传文件
	 */
	/*public function uploadFile(){
		$result = array();
		
		if (isset($_FILES['Filedata']) )
		{
			$file = $_FILES['Filedata']['tmp_name'];
			$error = false;
			$size = false;
			$toFile = dirname(__FILE__).'/../../Upload/ZS/'.$_FILES['Filedata']['name'];
			if( move_uploaded_file( $file, $toFile ) ){
				@chmod( $toFile, 0755 );		
			}else{
				$error = "Move uploaded file fail!";
			}
		
			$addr = gethostbyaddr($_SERVER['REMOTE_ADDR']);
		
			$log = fopen('script.log', 'a');
			fputs($log, ($error ? 'FAILED' : 'SUCCESS') . ' - ' . preg_replace('/^[^.]+/', '***', $addr) . ": {$_FILES['Filedata']['name']} - {$_FILES['Filedata']['size']} byte\n" );
			fclose($log);
		
			if ($error)
			{
				$result['result'] = 'failed';
				$result['error'] = $error;
			}
			else
			{
				$result['result'] = 'success';
				//$result['size'] = "Uploaded an image ({$size['mime']}) with  {$size[0]}px/{$size[1]}px.";
				$result['size'] = "Uploaded an file ok.";
			}
		
		}
		else
		{
			$result['result'] = 'error';
			$result['error'] = 'Missing file or internal error!';
		}
		
		
		if (!headers_sent() )
		{
			header('Content-type: application/json');
		}
		
		echo json_encode($result);
		exit;
	}
	*/
	/**
	 * 得到墙体彩绘的图片及说明
	 * Enter description here ...
	 */
	public function getImgContent(){
		$m_id=$_REQUEST["m_id"];
		$lan=$_REQUEST["lan"];
		$rev=array("url"=>"","text"=>"");
		$image=new Model("image");
		$query["pid"]=$m_id;
		$datas=$image->where($query)->select();
		if(is_array($datas) && count($datas)){
			$data=$datas[0];
			
			$rev["pid"]=$data["pid"];
			$rev["url"]=$data["img_url"];
			if($lan=="en"){
				$rev["text"]=unserialize($data["img_shuoming_en"]);
			}else{
				$rev["text"]=unserialize($data["img_shuoming"]);
			}
			
		}
		echo json_encode($rev);
		exit;
	}
	/**
	 * 添加墙体彩绘(中文)
	 */
	public function addQiangti(){
		$pid=$_REQUEST["pid"];
		$this->assign('pid',$pid);
		$this->assign("lan",'zh-cn');
		if($pid){
			$image=new Model("image");
			$query["pid"]=$pid;
			$rev=array();
			$datas=$image->where($query)->select();
			if(is_array($datas) && count($datas)){
				$data=$datas[0];
				
				$rev["pid"]=$data["pid"];
				$rev["url"]=$data["img_url"];
				$rev["text"]=unserialize($data["img_shuoming"]);
				
			}
			$this->assign('imageData',$rev);
		}
		$this->display("addQiangti");
	}
	/**
	 * 添加墙体彩绘(英文)
	 */
	public function addQiangtiEn(){
		$pid=$_REQUEST["pid"];
		$this->assign('pid',$pid);
		$this->assign("lan",'en');
		if($pid){
			$image=new Model("image");
			$query["pid"]=$pid;
			$rev=array();
			$datas=$image->where($query)->select();
			if(is_array($datas) && count($datas)){
				$data=$datas[0];
				
				$rev["pid"]=$data["pid"];
				$rev["url"]=$data["img_url"];
				$rev["text"]=unserialize($data["img_shuoming_en"]);
				
			}
			$this->assign('imageData',$rev);
		}
		$this->display("addQiangtiEn");
	}
	/**
	 * 删除墙体彩绘
	 */
	public function delQiangti(){
		$pid=$_REQUEST["pid"];
		$img=new Model("image");
		$rev=$img->where('pid='.$pid)->delete();
		echo $rev;
		exit;
	}
	/**
	 * 保存墙体彩绘的功能
	 */
	public function saveQiangti(){
		$data=$_REQUEST["data"];
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$file_size=ini_get('upload_max_filesize');
		$file_size=str_replace("M","",$file_size);
		$upload->maxSize  = $file_size*1024*1024 ;// 设置附件上传大小
		
		//$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$toFile = dirname(__FILE__).'/../../Upload/ZS/';
		$upload->savePath =  $toFile;// 设置附件上传目录
		$revI=true;
		if(!$upload->upload()) {// 上传错误提示错误信息
			
			//$this->error($upload->getErrorMsg());
			$revI=false;
		
		}else{// 上传成功 获取上传文件信息
		
			$info =  $upload->getUploadFileInfo();
		
		 }
		if($data["pid"]){//编辑
			if($revI){
				 if(is_array($info) && count($info)>0){
				 	$image=new Model('image');
				 	$imagename=$info[0]["savename"];
				 	$data["img_name"]=$imagename;
					$data["img_url"]='Upload/ZS/'.$imagename;
					if($data["m_bj"]=="en"){
						$data["img_shuoming_en"]=serialize($data["img_shuoming"]);
					}else{
						$data["img_shuoming"]=serialize($data["img_shuoming"]);
					}
					$data["g_id"]='0';
					
					$image->find($data["pid"]);
					$rev=$image->save($data);
					
				 }
			 }else{
			 	$image=new Model('image');
			 	if($data["m_bj"]=="en"){
					$data["img_shuoming_en"]=serialize($data["img_shuoming"]);
				}else{
					$data["img_shuoming"]=serialize($data["img_shuoming"]);
				}
				$data["g_id"]='0';
				
				$image->find($data["pid"]);
				$rev=$image->save($data);
			 }
		}else{
		 $rev="0";
		 if($revI){
			 if(is_array($info) && count($info)>0){
			 	$image=new Model('image');
			 	$imagename=$info[0]["savename"];
			 	$data["img_name"]=$imagename;
				$data["img_url"]='Upload/ZS/'.$imagename;
				if($data["m_bj"]=="en"){
					$data["img_shuoming_en"]=serialize($data["img_shuoming"]);
				}else{
					$data["img_shuoming"]=serialize($data["img_shuoming"]);
				}
				$data["g_id"]='0';
				
				$data["img_time"]=time();
				$rev=$image->add($data);
				

			 }
		 }
		}
		if($rev>=1){
			echo "<script> parent.showDialog(1);</script>";
		}else{
			echo "<script> parent.showDialog(0);</script>";
		}
		// echo json_encode($rev);
		 exit;
		
	}
}